﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Web.UI;
using Twipler.App;
using Twipler.App.DataDoc;
using Twipler.App.Entity;
using Twipler.App.Session;
using Twipler.Data.Cache;
using Twipler.Twitter;
using Twipler.Xtensions;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Current.SessionTrack.GetType() == typeof(InnerSessionEmpty))
        {
            if (!InnerSessionFactory.AreSessionsAvailable())
                Response.Redirect("settings/static/notavailable.htm");
            else
                Response.Redirect("settings/html/maintenance.htm?at=" + InnerSessionFactory.ServiceMaintenceAt().ToString("{0:yyyyMMddHHmm}"));
        }
        
        if (!IsPostBack)
        {
            chkFollow.Attributes["onclick"] = "setFollowCookie(this);";
        }

        if (Request.QueryString["msg"] != null)
        {
            if (Request.QueryString["msg"] == "ce")
            {
                litHeader.Text = "<script type=\"text/javascript\">alert('Incorrect username or password.');</script>  "; 
            }
            return;
        }

        // User logout
        if (Current.Visitor.IsAnonymous || (Request.QueryString.Count == 1 && Request.QueryString[0] == "lo"))
        {
            if (Request.Cookies["AutoLoginId"] != null)
		    {
		    	string[] credentials = Request.Cookies["AutoLoginId"].Value.Split(' ');
		    	if (credentials.Length == 2)
		    	{
		    		Twipler.App.Current.SessionTrack.Credentials = credentials;
		    		Response.Redirect("OAuthLogin.aspx?twip=true");
		    	}
            }

            if (!IsPostBack)
                Session.Abandon();

            timerTickNow.Enabled = true;
            pnlNotLoggedIn.Visible = true;
            return;
        }


        // Follow twipler :)
        if ((Current.Visitor != null) && (Request.Cookies["follow"] != null) && (string.Compare(Request.Cookies["follow"].Value, "true", StringComparison.OrdinalIgnoreCase)==0))
        {
            Friendships.Befriend("twipler");
        }

        // User does not have a design selected, redirect to selection.
        if ((Current.Visitor.Design == null) || (Current.Visitor.Design.FirstPage == null))
            Response.Redirect("settings/theme/select.aspx");

        // Redirect to page renderer
        Response.Redirect(Current.Visitor.Design.FirstPage.Name + ".html");
    
    }

    protected void btnTwipLogin_Click(object sender, EventArgs e)
    {
        Twipler.App.Current.SessionTrack.Credentials = new string[] { txtTwipScreenname.Text.Trim(), txtTwipPassword.Text.Trim() };
        Response.Redirect("OAuthLogin.aspx?twip=true");
    }

    protected void timerTickNow_Update(object sender, EventArgs e)
    {
        timerTickNow.Enabled = false;
        
        // Fancy jazz OAuth login
        string authUrl = Current.SessionTrack.ApiCaller.UserAuthenticateUrl();
        if (authUrl != string.Empty)
            litPage.Text = "<a class=\"bigbutton\" href=\"" + authUrl.FullUrlFromVirtualPath() + "\" onclick=\"return VerifyAgree('" + chkAgree.ClientID  + "');\">Login</a>";
        else
            litPage.Text = "<a class=\"bigbutton\" href=\"default.aspx?lo\">Retry<br/><p style=\"font-size:12px; margin: 4px 0px 0px 0px;\">Twitter is unreachable!</p></a>";
    }
}
